<template>
  <cloudPage :isLoading="isLoading">
    <cloudHeader slot="gHeader" background="#121212">
      <view class="grace-header-body">
        <!-- 返回按钮 -->
        <view
          class="grace-header-icons grace-icons icon-arrow-left"
          style="color: #fff"
          @tap.stop="handleNavBack"
        ></view>
        <!-- 中间内容 -->
        <view
          class="grace-header-content-noflex grace-text-center font-32"
          style="color: #fff; font-size: 36rpx"
          >{{ $t("mf.my.detail") }}</view
        >
        <!-- 右侧按钮 -->
        <view class="icons" style="font-size: 38rpx"> </view>
      </view>
    </cloudHeader>
    <view class="container">
      <order-detail-box :data="detail" />
      <view class="order_info">
        <view class="order_info_title">{{ $t("mf.order.order_info") }}</view>
        <view class="flex">
          <view class="flex1">
            <view class="info_text">{{ $t("mf.order.buy_date") }}</view>
            <view class="info_text">{{ $t("mf.order.mining_day") }}</view>
            <view class="info_text">{{ $t("mf.order.order_sn") }}</view>
          </view>
          <view class="">
            <view class="info_data">{{
              detail && detail.created_at | moment("YYYY.MM.DD HH:mm")
            }}</view>
            <view class="info_data">{{ detail && detail.shifang_day }}</view>
            <view class="info_data">{{ detail && detail.order_sn }}</view>
          </view>
        </view>
      </view>
      <view class="transfer_box">
        <button class="transfer_btn" @tap.stop="closeShade">
          {{ $t("mf.order.transfer") }}
        </button>
      </view>
    </view>
    <graceBottomDialog
      :show="showTransfer"
      @closeDialog="closeShade"
      background="rgba(0,0,0,0.3)"
    >
      <view slot="content">
        <view class="shade_bottom_box">
          <view class="cancel_text" @click="closeShade">{{
            $t("mf.my.cancel")
          }}</view>
          <view class="title"
            >{{ $t("mf.my.transfer1") }}{{ $t("mf.my.transfer2") }}</view
          >
          <input
            type="digit"
            class="transfer_input"
            :placeholder="$t('mf.my.transfer_input')"
            v-model="price"
          />
          <view class="proposal">{{
            $t("mf.my.proposal", { proposal: proposal })
          }}</view>
          <view class="flex">
            <button class="transfer_btn" @tap.stop="confirmTransfer">
              {{ $t("mf.my.confirm") }}
            </button>
          </view>
        </view>
      </view>
    </graceBottomDialog>
    <cloudPayPassword
      :show="showpay"
      @keyboardDone="payPasswordDone"
      @close="togglePayPassword"
    >
    </cloudPayPassword>
  </cloudPage>
</template>

<script>
import { mapActions } from "vuex";
import orderDetailBox from "../components/orderDetailBox";
export default {
  components: {
    orderDetailBox,
  },
  data() {
    return {
      detail: null,
      isLoading: false,
      showTransfer: false,
      price: null,
      showpay: false,
    };
  },
  async onLoad(option) {
    console.log(option.id);
    this.isLoading = true;
    this.detail = await this.orderDetail({
      id: option.id,
      type: "user",
    });
    this.isLoading = false;
  },
  methods: {
    ...mapActions("mf/my", ["orderDetail", "sellOrder", "loadOrderList"]),
    closeShade() {
      console.log(this.showTransfer);
      this.showTransfer = !this.showTransfer;
    },
    // 确认转让
    confirmTransfer() {
      if (!this.price) {
        this.makeToast({ title: "请输入正确的数量" });
        return;
      }
      if (parseFloat(this.price) > parseFloat(this.proposal)) {
        this.makeToast({ title: "转让不可大于建议转让金额" });
        return;
      }
      this.togglePayPassword();
      this.closeShade();
    },
    // 输入密码
    async payPasswordDone(data) {
      this.togglePayPassword();
      await this.sellOrder({
        type: "sell",
        order_id: this.detail.id,
        sell_price: this.price,
        pay_password: data.password,
      });
      this.makeToast({ title: "转让成功" });
      this.price = null;
      this.loadOrderList({ type: "is_reward" });
      setTimeout(() => {
        this.handleNavBack();
      }, 1500);
    },
    togglePayPassword() {
      this.showpay = !this.showpay;
    },
  },
  computed: {
    // 建议出售价格
    proposal() {
      if (!this.detail) {
        return;
      }
      let day = this.detail.cycle_day - this.detail.shifang_day;
      console.log(this.transfer_data, day);
      if (day > 2) {
        return parseFloat(
          (day / this.detail.cycle_day) * this.detail.price_total * 0.75
        ).toFixed(2);
      } else {
        return 0;
      }
    },
  },
};
</script>

<style scoped>
.container {
  width: 100%;
  box-sizing: border-box;
  padding: 40rpx 20rpx 0;
}
.order_info {
  width: 100%;
  height: 356rpx;
  background: linear-gradient(270deg, #1b1b2b 0%, #232534 100%);
  box-shadow: 0px 3px 4px 0px rgba(0, 0, 0, 0.5);
  border-radius: 12rpx;
  box-sizing: border-box;
  padding: 0 30rpx;
}
.order_info_title {
  width: 100%;
  height: 107rpx;
  box-sizing: border-box;
  border-bottom: 1px solid #151935;
  font-size: 30rpx;
  font-family: PingFangSC-Medium, PingFang SC;
  font-weight: 500;
  color: #ffffff;
  line-height: 107rpx;
  margin-bottom: 30rpx;
}
.info_text {
  height: 40rpx;
  font-size: 28rpx;
  font-family: PingFangSC-Medium, PingFang SC;
  font-weight: 500;
  color: rgba(255, 255, 255, 0.8);
  line-height: 40rpx;
  margin-bottom: 30rpx;
}
.info_data {
  height: 40rpx;
  font-size: 28rpx;
  font-family: PingFangSC-Medium, PingFang SC;
  font-weight: 500;
  color: #ffffff;
  line-height: 40rpx;
  margin-bottom: 30rpx;
  text-align: right;
}
.transfer_box {
  position: absolute;
  bottom: 30rpx;
  width: 100%;
  height: 110rpx;
  border-radius: 20rpx;
}
.transfer_btn {
  width: 516rpx;
  height: 100rpx;
  background-image: url("/static/auth/login/btn_bg.png");
  background-size: 100% 100%;
  background-repeat: no-repeat;
  border-radius: 55rpx;
  line-height: 100rpx;
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  font-size: 34rpx;
  font-family: PingFangSC-Medium, PingFang SC;
  font-weight: 500;
  color: #2b2c3e;
}
.shade_bottom_box {
  width: 100%;
  height: 511rpx;
  background-image: url("/static/mf/suanlibao/profit_tan.png");
  background-size: 100% 100%;
  background-repeat: no-repeat;
  position: relative;
  left: 50%;
  transform: translateX(-50%);
  padding: 60rpx 50rpx 0;
  box-sizing: border-box;
}
.shade_bottom_box .title {
  height: 48rpx;
  font-size: 34rpx;
  font-family: PingFangSC-Medium, PingFang SC;
  font-weight: 500;
  color: #ffffff;
  line-height: 48rpx;
  margin-bottom: 60rpx;
}
.transfer_input {
  width: 100%;
  height: 88rpx;
  line-height: 88rpx;
  box-sizing: border-box;
  font-size: 28rpx;
  padding: 0 0 30rpx;
  border-bottom: 1px solid rgba(254, 224, 150, 0.2);
  color: #fff;
  margin-bottom: 20rpx;
}
.proposal {
  height: 33rpx;
  font-size: 24rpx;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: rgba(254, 224, 150, 1);
  line-height: 33rpx;
  margin-bottom: 50rpx;
}
.transfer_btn1 {
  width: 516rpx;
  height: 100rpx;
  background: linear-gradient(270deg, #feecbf 0%, #ffdb86 100%);
  border-radius: 55rpx;
  font-size: 34rpx;
  font-family: PingFangSC-Medium, PingFang SC;
  font-weight: 500;
  color: #2b2c3e;
  line-height: 100rpx;
}
.cancel {
  border: 1px solid #ebebeb;
  box-sizing: border-box;
  background: rgba(0, 0, 0, 0);
  color: rgba(255, 255, 255, 0.8);
  margin-right: 30rpx;
}
.cancel_text {
  position: absolute;
  right: 50rpx;
  height: 40rpx;
  font-size: 28rpx;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: #ffffff;
  line-height: 40rpx;
}
</style>